<?php
$this->headTitle($this->translator('message_add'));
$this->headLink()
    ->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.all.css');
$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js');
?>
<?php if($this->enterprise) :?>
<script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/tinymce/tiny_mce.js"></script>
<div class="t_bizprofile_register">
    <h2><?php echo $this->translator('message_add') ?></h2>
    <?php // cho phep gui tin nhan ?>
    <?php if(!$this->permission || $this->permission->messaging_permission  == 1):?>
    <form method="post" action="" id="editEnterpriseInfoForm">
        <div class="clearfix">
            <?php echo $this->translator('message_caption_to'); ?>:
         </div>
        <div class="clearfix">
            <input type="text" disabled="" name="message_to_name" id="message_to_name" value="<?php echo $this->enterprise->company_name_vn; ?>" class="text" style="width:400px;" />
            <input type="hidden" name="message_to" id="message_to" value="<?php echo $this->enterprise->id; ?>" class="text" style="width:400px;" />
         </div>
        <div class="clearfix">
            <?php echo $this->translator('message_caption_title'); ?>:
         </div>
        <div class="clearfix">
            <input type="text" name="title" id="title" value="" class="text" style="width:400px;" />
            <font style="color:red; padding-left:5px;">(*)</font>
         </div>
         <div class="clearfix">
            <?php echo $this->translator('message_detail_content'); ?>:
        </div>
        <div class="clearfix">
            <textarea name="content" id="content" class="tinymce" style="width: 440px; height: 100px"></textarea>
        </div>
        <div class="clearfix">
            <?php echo $this->tokenElement; ?>
            <input type="hidden" name="info_id" value="<?php echo $this->info->info_id?>" />
            <button type="submit"><span><?php echo $this->translator('send'); ?></span></button>
        </div>
    </form>
    <?php else: // khong duoc phep gui tin nhan?>
    	<?php echo $this->translator('message_permission');?>
    <?php endif;?>
</div>
<div id="messageDialog"></div>

<script type="text/javascript">
/**
 * Upload handlers
 */
function onUploadComplete(event, ID, fileObj, response, data) {
    var images = $.evalJSON(response);

    var presentImage = $('<img/>');
    $(presentImage).attr('src', images.thumbnail.url)
        .attr('width', '100').attr('height', '100')
        .attr('style', 'cursor: pointer')
        .click(function() {
            selectImage(images);
        });
    var select = $('<select/>');
    $(select).attr('width', '100');
    $('<option/>').attr('value', '').html('---').appendTo(select);

    for (var thumb in images) {
        $('<option/>').attr('value', images[thumb].url).html(images[thumb].size).appendTo(select);
    }
    $(select).change(function() {
        if ($(this).val() != '') {
            insertImage($(this).val());
        }
    });

    $('<li/>').append(presentImage).append($('<br/>')).append(select).appendTo($('#photos'));
};

function selectImage(images) {
    var urls = {};
    for (var thumb in images) {
        urls[thumb] = images[thumb].url;
    }
    $('#articleImage').val($.toJSON(urls));
    $('#articleImageContainer').html('<img src="' + images.crop.url + '" />');
};

/**
 * Insert image to TinyMCE editor at current position
 * @param string url The image url
 */
function insertImage(url) {
    tinyMCE.getInstanceById('content').execCommand('mceInsertContent', false, '<img src="' + url + '" />');
};

/**
 * Init TinyMCE instances
 */
tinyMCE.init({
    theme: 'advanced',
    theme_advanced_toolbar_location: 'top',
    theme_advanced_toolbar_align: 'left',
    width: '620',
    height: '225',
    mode: 'exact',
    elements: 'description',
    content_css: '<?php echo $this->APP_STATIC_SERVER; ?>/skins/<?php echo $this->APP_TEMPLATE; ?>/<?php echo $this->APP_SKIN; ?>/default.css',
    relative_urls: false,
    remove_script_host: false
});

tinyMCE.init({
    theme: 'advanced',
    plugins: 'safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager,preelementfix',
    theme_advanced_buttons2: 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo',
    theme_advanced_buttons3: 'link,unlink,anchor,image,cleanup,code,|,preview,|,forecolor,backcolor',
    theme_advanced_buttons4: 'tablecontrols,ltr,rtl',  //"tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,fullscreen",
    theme_advanced_toolbar_location: 'top',
    theme_advanced_toolbar_align: 'left',
    width: '620',
    mode: 'exact',
    elements: 'content',
    content_css: '<?php echo $this->APP_STATIC_SERVER; ?>/skins/<?php echo $this->APP_TEMPLATE; ?>/<?php echo $this->APP_SKIN; ?>/default.css',
    relative_urls: false,
    remove_script_host: false,
    onchange_callback: function(editor) {
        tinyMCE.triggerSave();
        $('#' + editor.id).valid();
    }
});

/**
 * Insert clip to TinyMCE editor at current position
 */
function insertFile() {
    var htmlCode = $('#fileHtmlCode').attr('value');
    if (null != htmlCode && '' != htmlCode) {
        tinyMCE.getInstanceById('content').execCommand('mceInsertContent', false, '<div class="clearfix" style="text-align: center">' + htmlCode + '</div>');
    } else {
        var file   = ($('#fileUrl').attr('value') != null) ? $('#fileUrl').attr('value') : null;
        var image  = ($('#fileImage').attr('value') != null) ? $('#fileImage').attr('value') : null;
        var width  = ($('#fileWidth').attr('value') != null) ? $('#fileWidth').attr('value') : '450';
        var height = ($('#fileHeight').attr('value') != null) ? $('#fileHeight').attr('value') : '337';
        tinyMCE.getInstanceById('content').execCommand('mceInsertContent', false, '<div class="clearfix" style="text-align: center"><object width="' + width + '" height="' + height + '" type="application/x-shockwave-flash" data="<?php echo $this->APP_STATIC_SERVER; ?>/js/jwplayer/player.swf" style="visibility: visible"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="menu" value="false" /><param name="flashvars" value="play=true&repeat=true&autostart=false&file=' + file + '&image=' + image + '" /></object></div>');
    }
};

function onUploadCompleteFile(event, ID, fileObj, response, data) {
    var res = $.evalJSON(response);
    $('#fileUrl').attr('value', res.original.url);
};

function onUploadCompleteFileImage(event, ID, fileObj, response, data) {
    var res = $.evalJSON(response);
    $('#fileImage').attr('value', res.original.url);
};

$(document).ready(function() {
    /**
    * Validate form
    */
    $('#editEnterpriseInfoForm').validate({
        rules: {
            title: {
                required: true
            }
        },
        messages: {
            title: {
                required: "<?php echo $this->translator('title_require'); ?>"
            }
        }
    });
});
</script>
<?php else: ?>
    <?php echo $this->translator('enterprise_not_exist'); ?>
<?php endif; ?>